import { useUserStore } from "@/store/modules/user";
import axios, {
  AxiosError,
  AxiosRequestConfig,
  InternalAxiosRequestConfig,
} from "axios";

const service = axios.create({
  // baseURL: 'http://localhost:8081',
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 100000,
  withCredentials: true,
});

// request 接口请求拦截
service.interceptors.request.use(
  //   (config: AxiosRequestConfig) => {
  (config: InternalAxiosRequestConfig) => {
    const userStore = useUserStore();
    const token: string = userStore.token;
    //自定义请求头
    if (token) {
      config.headers!["AccessToken"] = token;
      //   config.headers!.AccessToken = token;
    }
    return config;
  },
  (error: AxiosError) => {
    //请求错误
    return Promise.reject(error);
  }
);

export default service;
